package com.china08.yxyapi.api.manage.edu.platform.continuedu.lesson;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonItem;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonsVideoRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduLessonCrtItemController extends YxyApiBaseController {

    @Autowired
    YxyContinueduLessonItemRepository itemRepository;

    @Autowired
    YxyContinueduLessonRepository lessonRepository;

    @Autowired
    YxyContinueduLessonsVideoRepository videoRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/lesson/crt/item", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String title = reqModel.title;
        String videoId = reqModel.videoId;
        Integer orderNum = itemRepository.countByLessonId(lessonId) + 1;

        // 创建课时
        YxyContinueduLessonItem item = new YxyContinueduLessonItem();
        item.setLessonId(lessonId);
        item.setTitle(title);
        item.setVideoId(videoId);
        item.setOrderNum(orderNum);
        itemRepository.saveAndFlush(item);

        // 计算总时常
        Integer duration = 0;
        List<YxyContinueduLessonItem> itemList = itemRepository.findByLessonId(lessonId);
        for (YxyContinueduLessonItem it : itemList) {
            String vId = it.getVideoId();
            YxyContinueduLessonsVideo video = videoRepository.findById(vId);
            if (video != null) {
                duration = duration + video.getDuration();
            }
        }

        // 修改课程课时数
        YxyContinueduLesson lesson = lessonRepository.findById(lessonId);
        if (lesson != null) {
            lesson.setCount(orderNum);
            lesson.setLastUpDate(new Date());
            lesson.setDuration(duration);
        }
        // 保存课程
        lessonRepository.save(lesson);
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public String title;
        public String videoId;

        @Override
        public void autoCheck() {
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String id;
    }
}
